package org.seamon.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.seamon.management.mapper.RoleMenuMapper;
import org.seamon.management.pojo.dto.RoleMenuDeleteDto;
import org.seamon.management.pojo.dto.RoleMenuUpdateDto;
import org.seamon.management.pojo.entity.RoleMenu;
import org.seamon.management.service.RoleMenuService;
import org.seamon.management.util.CacheConstants;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Simon
* @description 针对表【sys_role_menu(角色菜单关联表)】的数据库操作Service实现
* @createDate 2025-07-11 00:31:54
*/
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu>
    implements RoleMenuService{

    @Override
    @CacheEvict(cacheNames = {CacheConstants.ROLE_MENU_LIST}, key = "#dto.roleId")
    public void updateMenu(RoleMenuUpdateDto dto) {
        List<RoleMenu> roleMenus = new ArrayList<>();
        dto.getMenuIds().forEach(menuId -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(dto.getRoleId());
            roleMenu.setMenuId(menuId);
            roleMenus.add(roleMenu);
        });
        this.getBaseMapper().insert(roleMenus);
    }

    @Override
    @CacheEvict(cacheNames = {CacheConstants.ROLE_MENU_LIST}, key = "#dto.roleMenuIds")
    public void remove(RoleMenuDeleteDto dto) {
        List<Long> roleMenuIds = dto.getRoleMenuIds();
        this.getBaseMapper().deleteByIds(roleMenuIds);
    }

    @Override
    public List<Long> getMenuByRoleIds(List<Long> roleIds) {
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(RoleMenu::getRoleId, roleIds)
                .select(RoleMenu::getMenuId);
        List<RoleMenu> list = this.list(queryWrapper);
        return list.stream()
                .map(RoleMenu::getMenuId)
                .distinct()
                .toList();
    }

}




